package switchviz.graphic.gauge.tacho.scale;

import java.util.StringTokenizer;

import org.jdom.Element;


public class TachoScalePersistence 
{
	private final static String SCALESTART = "scalestart";
	private final static String ANGLE = "angle";
	private final static String DEPTH = "depth";
	private final static String NUMSMALLTICKS = "numsmallticks";
	private final static String NUMBIGTICKS = "numbigticks";
	private final static String LENGTHBIGTICKS = "lengthbigticks";
	private final static String LENGTHSMALLTICKS = "lengthsmallticks";
	private final static String WIDTHBIGTICKS = "widthbigticks";
	private final static String WIDTHSMALLTICKS = "widthsmallticks";
	private final static String SCALECOLOR = "scalecolor";
	private final static String LABELCOLOR = "labelcolor";

	private Element _root = null;
	
	public TachoScalePersistence(Element root)
	{
		_root = root;
	}
	
	public Element getElement()
	{
		return _root;
	}
	
	public void setElement(Element node)
	{
		_root = node;
	}
	
	public TachoScaleDataModel getDataModel(Element node)
	{
		_root = node;
		
		return getDataModel();
	}
	
	public TachoScaleDataModel getDataModel()
	{
		TachoScaleDataModel datamodel = new TachoScaleDataModel();
		
		if(_root.getChild(SCALESTART) != null)
			datamodel.setScaleStart(Float.valueOf(_root.getChildText(SCALESTART)));
		if(_root.getChild(ANGLE) != null)
			datamodel.setAngle(Float.valueOf(_root.getChildText(ANGLE)));
		if(_root.getChild(DEPTH) != null)
			datamodel.setDepth(Float.valueOf(_root.getChildText(DEPTH)));
		if(_root.getChild(NUMBIGTICKS) != null)
			datamodel.setNumbigticks(Integer.valueOf(_root.getChildText(NUMBIGTICKS)));
		if(_root.getChild(NUMSMALLTICKS) != null)
			datamodel.setNumsmallticks(Integer.valueOf(_root.getChildText(NUMSMALLTICKS)));
		if(_root.getChild(LENGTHBIGTICKS) != null)
			datamodel.setLengthbigticks(Float.valueOf(_root.getChildText(LENGTHBIGTICKS)));
		if(_root.getChild(LENGTHSMALLTICKS) != null)
			datamodel.setLengthsmallticks(Float.valueOf(_root.getChildText(LENGTHSMALLTICKS)));
		if(_root.getChild(WIDTHBIGTICKS) != null)
			datamodel.setWidthbigticks(Float.valueOf(_root.getChildText(WIDTHBIGTICKS)));
		if(_root.getChild(WIDTHSMALLTICKS) != null)
			datamodel.setWidthsmallticks(Float.valueOf(_root.getChildText(WIDTHSMALLTICKS)));
		if(_root.getChild(SCALECOLOR) != null)
			datamodel.setScaleColor(getColorCode(_root.getChildText(SCALECOLOR)));
		if(_root.getChild(LABELCOLOR) != null)
			datamodel.setLabelColor(getColorCode(_root.getChildText(LABELCOLOR)));
		
		return datamodel;
	}
	
	public Element getElement(TachoScaleDataModel datamodel)
	{
		addNode(SCALESTART, String.valueOf(datamodel.getScaleStart()));
		addNode(ANGLE, String.valueOf(datamodel.getAngle()));
		addNode(DEPTH, String.valueOf(datamodel.getDepth()));
		addNode(NUMBIGTICKS, String.valueOf(datamodel.getNumbigticks()));
		addNode(NUMSMALLTICKS, String.valueOf(datamodel.getNumsmallticks()));
		addNode(LENGTHBIGTICKS, String.valueOf(datamodel.getLengthbigticks()));
		addNode(LENGTHSMALLTICKS, String.valueOf(datamodel.getLengthsmallticks()));
		addNode(WIDTHBIGTICKS, String.valueOf(datamodel.getWidthbigticks()));
		addNode(WIDTHSMALLTICKS, String.valueOf(datamodel.getWidthsmallticks()));
		String colorcode = datamodel.getScaleColor()[0] + ";" + datamodel.getScaleColor()[1] + ";" + datamodel.getScaleColor()[2];
		addNode(SCALECOLOR, colorcode);
		colorcode = datamodel.getLabelColor()[0] + ";" + datamodel.getLabelColor()[1] + ";" + datamodel.getLabelColor()[2];
		addNode(LABELCOLOR, colorcode);
		
		
		return _root;
	}
	
	private void addNode(String nodename, String value)
	{
		if(_root.getChild(nodename) != null)
			_root.getChild(nodename).setText(value);
		else
			_root.addContent(new Element(nodename).setText(value));
	}
	
	private float[] getColorCode(String colorcode)
	{
		float[] color = {0.0f, 0.0f, 0.0f};
		StringTokenizer tokenizer = new StringTokenizer(colorcode, ";", false);
		int i = 0;
		while(tokenizer.hasMoreTokens())
		{
			if(i == 3) break;
			color[i] = Float.valueOf(tokenizer.nextToken());
			++i;
		}
		return color;
	}
}
